package com.test.auth_db_auto.support.code;

import com.test.auth_db_auto.utils.RedisUtil;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @ Author: .Mr
 * @ ClassName RedisCodeService
 * @ Description TODO
 * 用户登录信息保存到redis
 **/
public class RedisCodeService implements BootCodeService<String> {

    //    private StringRedisTemplate template;
    private long expire;

    public RedisCodeService( long expire) {
//        this.template = template;
        this.expire = expire;
    }

    @Override
    public String getCodeValue(String key) {
        return RedisUtil.get(key).toString();
//        return this.template.opsForValue().get(key);
    }

    @Override
    public void setCodeValue(String key, String value) {
        RedisUtil.set(key,value,this.expire);
//        this.template.opsForValue().set(key, value, this.expire, TimeUnit.SECONDS);
    }

    @Override
    public boolean verification(String key, String value, boolean ignore) {

        if (value == null || value.trim().equals("")) {
            return false;
        }

        Boolean has = RedisUtil.hasKey(key);

        if (has == null || !has) {
            return false;
        }

        Long expire = RedisUtil.getExpire(key);

        if (expire == null || expire <= 0) {
            return false;
        }

        String tmpValue = this.getCodeValue(key);

        if (ignore) {
            return value.equalsIgnoreCase(tmpValue);
        }
        return value.equals(tmpValue);
    }


}
